rm(list=ls())
library(data.table)
library(plyr)
library(tidyr)
library(lfe)
library(stargazer)
library(xtable)
library(sandwich)
library(roll)
library(readxl)
library(readr)
library(zoo)
library(texreg)
library(DescTools) 

m <- data.table(read_xlsx("../Data/MasterData.xlsx", skip = 1, guess_max = 1e4))
m[, age := 2021 - year]

m[, memrableperiod_pret := memrableperiod_pret*100]
m[, memrableperiod_aret := memrableperiod_aret*100]

m[, retrecall_1day_rate := retrecall_1day_rate*100]
m[, retrecall_30day_rate := retrecall_30day_rate*100]
m[, retrecall_1year_rate := retrecall_1year_rate*100]
m[, retrecall_5year_rate := retrecall_5year_rate*100]

m[, expret30day_market_rate_error := expret30day_market_rate_error*100]
m[, expret1year_market_rate_error := expret1year_market_rate_error*100]

###########################################################################
# Explain expectation
###########################################################################

m[, bias := (memrableperiod_pret-memrableperiod_aret)]


tmp <- m[!is.na(memrableperiod_pret)]$memrableperiod_pret
quantile(tmp, c(.25,.75))


m[, memrableperiod_pret_length := memrableperiod_pret * memrableperiod_length]

# free recall
f1 <- felm(expret30day_market_rate_error ~ memrableperiod_pret|age+gender+education+total_wealth+total_income+
             accountcheck_freq+newscheck_freq+discussion_freq+num_wechat|0|date, m[type==0])

f2 <- felm(expret1year_market_rate_error ~ memrableperiod_pret|age+gender+education+total_wealth+total_income+
             accountcheck_freq+newscheck_freq+discussion_freq+num_wechat|0|date, m[type==0])



f3 <- felm(expret30day_market_rate_error ~ memrableperiod_pret + memrableperiod_length + memrableperiod_pret_length|age+gender+education+total_wealth+total_income+
             accountcheck_freq+newscheck_freq+discussion_freq+num_wechat|0|date, m[type==0])

f4 <- felm(expret1year_market_rate_error ~ memrableperiod_pret + memrableperiod_length + memrableperiod_pret_length|age+gender+education+total_wealth+total_income+
             accountcheck_freq+newscheck_freq+discussion_freq+num_wechat|0|date, m[type==0])

stargazer(f1,f2,
          align=TRUE, dep.var.labels.include=TRUE,
          covariate.labels = c("Recalled return, Free Reall"),
          omit.stat=c("LL","ser","F","rsq"), ord.intercepts=FALSE, no.space=TRUE,
          single.row=FALSE, column.sep.width = "0pt", digits = 2)

# probed recall
tmp <- m[!is.na(retrecall_30day_rate)]$retrecall_30day_rate
quantile(tmp, c(.25,.75))

tmp <- m[!is.na(retrecall_1year_rate)]$retrecall_1year_rate
quantile(tmp, c(.25,.75))

f1 <- felm(expret30day_market_rate_error ~ retrecall_30day_rate|age+gender+education+total_wealth+total_income+
             accountcheck_freq+newscheck_freq+discussion_freq+num_wechat|0|date, m)
f2 <- felm(expret30day_market_rate_error ~ retrecall_1year_rate|age+gender+education+total_wealth+total_income+
             accountcheck_freq+newscheck_freq+discussion_freq+num_wechat|0|date, m)
f3 <- felm(expret30day_market_rate_error ~ retrecall_30day_rate + retrecall_1year_rate |age+gender+education+total_wealth+total_income+
             accountcheck_freq+newscheck_freq+discussion_freq+num_wechat|0|date, m)


f4 <- felm(expret1year_market_rate_error ~ retrecall_30day_rate|age+gender+education+total_wealth+total_income+
             accountcheck_freq+newscheck_freq+discussion_freq+num_wechat|0|date, m)
f5 <- felm(expret1year_market_rate_error ~ retrecall_1year_rate|age+gender+education+total_wealth+total_income+
             accountcheck_freq+newscheck_freq+discussion_freq+num_wechat|0|date, m)
f6 <- felm(expret1year_market_rate_error ~ retrecall_30day_rate + retrecall_1year_rate|age+gender+education+total_wealth+total_income+
             accountcheck_freq+newscheck_freq+discussion_freq+num_wechat|0|date, m)

stargazer(f1,f2,f3,f4,f5,f6,
          align=TRUE, dep.var.labels.include=TRUE,
          covariate.labels = c("Recalled own return, 1M", "Recalled own return, 1Y"),
          omit.stat=c("LL","ser","F","rsq"), ord.intercepts=FALSE, no.space=TRUE,
          single.row=FALSE, column.sep.width = "0pt", digits = 2)

